#include <iostream>
#include <signal.h>
#include "enet/enet.h"
#include "KMNetServer.h"
#include "KMNetConnectedClient.h"
#include "KMNetMsgIn.h"

enum
{
    CSLogin             = 0x0001,
    SCLoginResponse     = 0x0002,
    CSLogoff            = 0x0003,
    SCLogoffResponse    = 0x0004,
    CSSendText          = 0x0005,
    SCSendTextResponse  = 0x0006,
    SCBlastText         = 0x0007,
};

class KMNetMsgIn;

class ChatServer : public KMNetServer
{
protected:
    virtual void clientConnected(KMNetConnectedClient *client)
    {
        std::cout << "a client connected" << std::endl;
    }

    virtual void clientDisconnected(KMNetConnectedClient *client)
    {
        std::cout << "a client disconnected" << std::endl;
    }

    virtual void processMessageFromClient(KMNetConnectedClient *client, KMNetMsgIn &msg)
    {
    }
};

bool doLoop = true;
void doExit(int)
{
    doLoop = false;
};

int main()
{
    signal(SIGINT, doExit);
    signal(SIGTERM, doExit);

    enet_initialize();

    ChatServer *server = new ChatServer();

    if( !server->listenTo(9999) )
    {
        std::cout << "server failed to start" << std::endl;
    }
    else
    {
        while(doLoop)
        {
            server->update(100);
        }
        server->stopListen();
    }

    delete server;
    enet_deinitialize();
    std::cout << "exit.." << std::endl;
    return 0;
}
